<template>
  <div class="pulldown">
    <div class="pulldown-bswrapper" ref="bsWrapper">
      <div class="pulldown-scroller">
        <div class="pulldown-wrapper">
          <div v-html="tipText" style="float:left"></div>
          <div class="pulldown-wrapper-img" :style="{'background-image':'url('+imgSrc+')','float':'left'}"></div>
        </div>
        <ul class="pulldown-list">
          <li :key="i" class="pulldown-list-item" v-for="i of dataList">{{ `I am item ${i} ` }}</li>
        </ul>
      </div>
    </div>
  </div>
</template>
<script>
import BScroll from "@better-scroll/core";
import PullDown from "@better-scroll/pull-down";
BScroll.use(PullDown);
export default {
  data() {
    return { dataList: [], tipText: "下拉刷新", imgSrc: "./images/doge/begin.jpg" };
  },
  methods: {
    pullingDownHandler() {
      this.tipText = `<i class="el-icon-loading"></i>刷新中`;
      this.imgSrc = "./images/doge/ref.gif";
      setTimeout(() => {
        this.tipText = `<i class="el-icon-check"></i>刷新完毕`;
        this.imgSrc = "./images/doge/ok.jpg";
        this.finishPullDown();
      }, 1000);
    },
    finishPullDown() {
      setTimeout(() => {
        this.bscroll.finishPullDown();
        this.bscroll.refresh();
      }, 200);
    },
    scrollHandler() {},
  },
  mounted() {
    for (let i = 0; i < 100; i++) {
      this.dataList.push(i);
    }
    this.$nextTick(() => {
      this.bscroll = new BScroll(this.$refs.bsWrapper, {
        scrollY: true,
        bounceTime: 100,
        useTransition: false,
        disableTouch:false,
        pullDownRefresh: {
          threshold: 110,
          stop: 90,
        },
      });
      this.bscroll.on("pullingDown", this.pullingDownHandler);
      this.bscroll.on("enterThreshold", () => {
        this.tipText = `<i class="el-icon-bottom"></i>下拉刷新`;
        this.imgSrc = "./images/doge/begin.jpg";
      });
      this.bscroll.on("leaveThreshold", () => {
        this.tipText = `<i class="el-icon-top"></i>释放刷新`;
        this.imgSrc = "./images/doge/end.jpg";
      });
    });
  },
};
</script>
<style scoped>
.pulldown {
  height: 100%;
}
.pulldown-bswrapper {
  position: relative;
  height: 100%;
  overflow: hidden;
}
.pulldown-wrapper {
  position: absolute;
  top: 0;
  left: 50%;
  line-height: 100px;
  font-size: 20px;
  transform: translate(-50%, -100%);
  margin: auto;
  width: 200px;
}
.pulldown-wrapper-img{
    height: 100px;
    width: 100px;
    background-position: center;
    background-size: cover;
}
</style>
